package me.zhengjie.util;

import me.zhengjie.modules.system.domain.SafeRiskType;
import me.zhengjie.modules.system.service.SafeTestTypeService;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CacheUtil
 *
 * @author Jin
 */
public class CacheUtil {

    private final SafeTestTypeService safeTestTypeService;

    // 节点数据
    public List<SafeRiskType> safeRiskTypeList = new ArrayList<>();
    // <节点id,节点信息>
    public Map<String, SafeRiskType> safeRiskTypeMap = new HashMap<>();
    // <节点id,节点名称>
    public Map<String, String> safeNameMap = new HashMap<>();
    // <所有节点mc,节点ids>
    public Map<String, String> safeNameIdMap = new HashMap<>();

    public CacheUtil(SafeTestTypeService safeTestTypeService) {
        this.safeTestTypeService = safeTestTypeService;
        this.safeRiskTypeList = safeTestTypeService.query();
        this.safeRiskTypeList.stream().forEach(data -> {
            safeRiskTypeMap.put(data.getId(), data);
            safeNameMap.put(data.getId(), data.getName());
            if (!StringUtils.isEmpty(data.getName())) {
                if (!safeNameIdMap.containsKey(data.getName())) {
                    safeNameIdMap.put(data.getName(), data.getId());
                } else {
                    String ids = safeNameIdMap.get(data.getName()) + "," + data.getId();
                    safeNameIdMap.put(data.getName(), ids);
                }
            }
        });
    }

    /**
     * 关键字匹配
     * @param value 关键字
     * @return 匹配的数据
     */
    public List<SafeRiskType> getContainList(String value) {
        List<SafeRiskType> safeRiskTypes = new ArrayList<>();
        this.safeRiskTypeList.stream().forEach(data -> {
            if(value.contains(data.getName())){
                safeRiskTypes.add(data);
            }
        });
        return safeRiskTypes;
    }
}
